<?php
/**
 * Created by PhpStorm.
 * User: wangjinbao
 * Date: 17/11/20
 * Time: 下午4:23
 */
use Illuminate\Http\Request;
//创建websocket服务器对象，监听0.0.0.0:9502端口
$ws = new swoole_websocket_server("0.0.0.0", 9502);
//$_REQUEST;
//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request) {
    //print_r($_REQUEST);exit;
    //print_r($request);exit;
    //var_dump($request->fd, $request->get, $request->server);
    $ws->push($request->fd, "hello, welcome_wjb\n");
});

//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
//    print_r($frame);exit;
//    echo "Message: {$frame->data}\n";
    $data=json_decode($frame->data);
//print_r($data);exit;
    $ws->push($data->toid, "server: {$frame->data}");
    //$ws->push($frame->fd, "server: {$frame->data}");
});

//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
    echo "client-{$fd} is closed\n";
});

$ws->start();